#ifndef MY_INTERPRETER_H
#define MY_INTERPRETER_H

#include "interpreter.h"
#include "verifier.h"
#include "myaction.h"

class MyInterpreter : public Interpreter{
    private:
        void beautification(map<int, Trace> &traces, int _id){
            Beautification::position(traces[_id]);
            if ( traces[_id].size() == 2 ){
                vector<Trace> ts;
                for (map<int, Trace>::iterator it=traces.begin(); it != traces.end(); it++){
                    ts.push_back(it->second);
                }
                if (traces.size() > 1){
                    Beautification::connections(ts, traces[_id]);
                    for (int i = 0; i < ts.size(); i++){
                        traces[ts[i].getId()] = ts[i];
                    }
                }
            }   
            else{
                traces.erase(_id);
            } 
        }
    
    public:
        MyInterpreter(){}
        ~MyInterpreter(){}
        
        void finishTrace(int _id){
            map<int, Trace> &traces = this->sketch.getTraces();
            traces[_id].filter();
            
            // distancia em pixels
            if (traces[_id][0].distance(traces[_id][-1]) > 20){
                beautification(traces, _id);
                PolygonVerifier verifier;
                if( verifier.verifySketch(this->sketch) ){
                    PolygonAction action;
                    action.setVertices(verifier.getVertices());
                    action.setEdges(verifier.getEdges());
                    action.setScene(scene);
                    action.act(this->sketch);
                }
            }
            else{
                traces.erase(_id);
            }
        }
};

#endif /* MY_INTERPRETER_H */
